﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;

public partial class Controls_ControlTinTucContent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_Data();
        }
    }

    /// <summary>
    /// Phuong thuc load du lieu vao Control<Literal></Literal>
    /// </summary>
    protected void Bind_Data()
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and TrangThai = 1";
        string strHTML = "";
        // Lay danh sach Tin tuc gom 4 tin voi dieu kien TrangThai = 1
        TList<TinTuc> Tlist_TinTuc = DataRepository.TinTucProvider.GetPaged(WhereClause, "Id DESC", 0, 4, out ToTalRows);
        if (Tlist_TinTuc.Count > 0)
        {
            // Duyet trong truong hop TListTinTuc > 0
            for (int i = 0; i < Tlist_TinTuc.Count; i++)
            {
                strHTML += "<div class=\"sub-tin-tuc\">";
                strHTML += "<div>";
                strHTML += "<img title='"+ Tlist_TinTuc[i].TomTat +"' src='Upload/TinTuc/" + Tlist_TinTuc[i].AnhDaiDien + "' /></div>";
                strHTML += "<div>";
                strHTML += "<a href='" + Tlist_TinTuc[i].Id + "_" + GetTieuDe(Tlist_TinTuc[i].TieuDe) + "_TinTuc.aspx'>";
                strHTML += "<p>" + formatString(Tlist_TinTuc[i].TieuDe.ToString(), 45) + "</p>";
                strHTML += "</a>";
                strHTML += "</div>";
                strHTML += "</div>";
                // Chen du lieu vao Literal : subTintucContent
                subTintucContent.Text = strHTML;

            }
        }
    }

    /// <summary>
    /// Phuong thuc chuyen tieu de co dau thanh khong dau de dua vao link
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    /// <summary>
    /// Format cat chuoi
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }
}